//
//  ViewController.m
//  QuickUseMasonry
//
//  Created by Sean Sebastian on 4/25/16.
//  Copyright © 2016 Sean Sebastian. All rights reserved.
//

#import "ViewController.h"

#import "Masonry.h"

/*
    关于Masonry框架中的两个经典问题

    1、在Masonry中能够添加autolayout约束有三个函数
    - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
    - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
    - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block; 

    mas_makeConstraints 只负责新增约束 Autolayout不能同时存在两条针对于同一对象的约束 否则会报错
    mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况
    mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束
    三种函数善加利用 就可以应对各种情况了

    2、equalTo 和 mas_equalTo的区别在哪里呢? 
     其实 mas_equalTo是一个MACRO（宏定义）
 
     #define mas_equalTo(...)                 equalTo(MASBoxValue((__VA_ARGS__)))
     #define mas_greaterThanOrEqualTo(...)    greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
     #define mas_lessThanOrEqualTo(...)       lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
     #define mas_offset(...)                  valueOffset(MASBoxValue((__VA_ARGS__)))
 
     可以看到 mas_equalTo只是对其参数进行了一个BOX操作(装箱) MASBoxValue的定义具体可以看看源代码 
 
     所支持的类型 除了NSNumber支持的那些数值类型之外 就只支持CGPoint CGSize UIEdgeInsets
 */

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self test4];
}

- (UIView *)viewMakeWithColor:(UIColor *)color
{
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = color;
    return view;
}


// 初级
// 显示一个居中的view
- (void)test1
{
    
    // 使用约束之后，基本可以抛弃CGRectMake了
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor blueColor];
    
    // 在做autoLayout之前 一定要先将view看驾到superview中 否则会报错
    [self.view addSubview:view];
    
    // mas_makeConstraints就是Masonry的autolayout添加函数 将所需的约束添加到block中就行了
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(300, 300));
    }];
}

// 让一个view略小于其superView（边距为30）
- (void)test2
{
    
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor blueColor];
    
    [self.view addSubview:view1];
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(300, 300));
    }];
    
    
    UIView *view2 = [[UIView alloc] init];
    view2.backgroundColor = [UIColor orangeColor];
    
    [view1 addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        // 使其边距为30
        // 三种写法：
        
        // I
        //        make.top.equalTo(view1).with.offset(10);
        //        make.left.equalTo(view1).with.offset(10);
        //        make.bottom.equalTo(view1).with.offset(-10);
        //        make.right.equalTo(view1).with.offset(-10);
        
        
        // II       可读性差
        //        make.top.left.bottom.and.right.equalTo(view1).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
        
        
        // III      最优
        //        make.edges.equalTo(view1).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
        make.edges.equalTo(view1).insets(UIEdgeInsetsMake(10, 10, 10, 10));
        
        // 其实调用 with 函数什么事情都没有做的，可以省略
        
    }];

}

// 让两个高度为150的view垂直居中且等宽且等间隔排列 间隔为10(自动计算其宽度)
- (void)test3
{
    int padding = 10 ;
    UIView *view1 = [self viewMakeWithColor:[UIColor yellowColor]];
    [self.view addSubview:view1];
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(300, 300));
    }];
    
    UIView *view2 = [self viewMakeWithColor:[UIColor whiteColor]];
    [view1 addSubview:view2];
    
    
    UIView *view3 = [self viewMakeWithColor:[UIColor blueColor]];
    [view1 addSubview:view3];
    
    
    
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view1.mas_centerY);
        make.left.equalTo(view1.mas_left).with.offset(padding);
        make.right.equalTo(view3.mas_left).with.offset(-padding);
        make.height.mas_equalTo(@150);
        
        make.width.equalTo(view3.mas_width);
    }];
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view1.mas_centerY);
        make.left.equalTo(view2.mas_right).with.offset(padding);
        make.right.equalTo(view1.mas_right).with.offset(-padding);
        make.height.mas_equalTo(view2);
        make.width.mas_equalTo(view2);
    }];
    
}

// 中级
// 在UIScrollView顺序排列一些view并自动计算contentSize
// 关键就在于container这个view起到了一个中间层的作用 能够自动的计算uiscrollView的contentSize
// 设置了在scrollView里面的container的约束之后就相当于设置了contentSize (test5验证)
- (void)test4
{
    UIView *view = [self viewMakeWithColor:[UIColor blackColor]];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(300, 300));
    }];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor whiteColor];
    [view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(5, 5, 5, 5));
//        make.edges.equalTo(view).with.insets(UIEdgeInsetsMake(5, 5, 5, 5));
    }];
    
    UIView *container = [[UIView alloc] init];
    [scrollView addSubview:container];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView); 
        make.width.equalTo(scrollView);
    }];
    int count = 10;
    UIView *lastView = nil;
    for (int i = 1 ; i <= count; ++i) {
        UIView *subv = [[UIView alloc] init];
        [container addSubview:subv];
        subv.backgroundColor = [UIColor colorWithHue:(arc4random() % 256 / 256.0)
                                          saturation:(arc4random() % 128 / 256.0) + 0.5
                                          brightness:(arc4random() % 128 / 256.0) + 0.5
                                               alpha:1];
        
        [subv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.width.equalTo(container);
            make.height.mas_equalTo(@(20*i));
            
            if(lastView)
            {
                make.top.mas_equalTo(lastView.mas_bottom);
            }
            else
            {
                make.top.mas_equalTo(container.mas_top);
            }
        }];
         
         lastView = subv;
    }
         
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lastView.mas_bottom);
    }];
}

- (void)test5
{
    UIView *view = [self viewMakeWithColor:[UIColor blackColor]];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(300, 300));
    }];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor whiteColor];
    [view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(5, 5, 5, 5));
        //        make.edges.equalTo(view).with.insets(UIEdgeInsetsMake(5, 5, 5, 5));
    }];
    
    UIView *container = [[UIView alloc] init];
    container.backgroundColor = [UIColor grayColor];
    [scrollView addSubview:container];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
        make.height.mas_equalTo(@1000);
    }];

}




@end
